{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1595827092327",
   "display_name": "Python 3.7.3 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import jieba\n",
    "import pyecharts\n",
    "from collections import Counter\n",
    "from pyecharts.charts import Line\n",
    "from pyecharts.charts import Bar\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_json('cnblog_article.json', encoding='utf-8')\n",
    "# df = df[['postdate', 'views']]\n",
    "df = df.drop(['title', 'sumary', 'posttime', 'views'], axis=1)\n",
    "df['count'] = 1\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['postdate'] = pd.to_datetime(df['postdate'])\n",
    "df = df.set_index('postdate')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = df.resample('M').sum()\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "x1, y1 = [], []\n",
    "for i in result.index:\n",
    "    x1.append(i)\n",
    "for i in result.values:\n",
    "    y1.append(i[0])\n",
    "print(x1, y1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "\n",
    "c = (\n",
    "    Bar()\n",
    "    .add_xaxis(x1)\n",
    "    .add_yaxis(\"商家A\", y1)\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    .render_notebook()\n",
    ")\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "week_data = []\n",
    "for i in df_json['postdate']:\n",
    "    week_data.append(datetime.strptime(i, \"%Y-%m-%d\").weekday())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "time_data = []\n",
    "for i in df_json['posttime']:\n",
    "    time_data.append(i.split(\":\")[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tc = Counter(time_data)\n",
    "# time_count_date = [tc[i] for i in tc]\n",
    "# time_count_date\n",
    "tc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "map_data = []\n",
    "for item in df_json.values:\n",
    "    week = datetime.strptime(item[2], \"%Y-%m-%d\").weekday()\n",
    "    print(week)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "line = Bar()\n",
    "# x_data = [i for i in date_counter]\n",
    "# y_data = [date_counter[i] for i in date_counter]\n",
    "line.add_xaxis(time_data)\n",
    "line.add_yaxis(\"count\", time_count_date)\n",
    "line.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import jieba\n",
    "import pyecharts\n",
    "from collections import Counter\n",
    "from pyecharts.charts import WordCloud\n",
    "from pyecharts.charts import Line\n",
    "\n",
    "# 获取所有字段存为一个字符串\n",
    "def get_text(json_file, column='title'):\n",
    "    df_json = pd.read_json(json_file, encoding='utf-8')\n",
    "    text = ''\n",
    "    for i in df_json[column]:\n",
    "        text += i\n",
    "    return text\n",
    "\n",
    "# 去停用词，使用jieba分词\n",
    "def split_word(text):\n",
    "    word_list = list(jieba.cut(text))\n",
    "    # 去掉一些无意义的词和符号，我这里自己整理了停用词库\n",
    "    with open('stop_word.txt', encoding='utf-8') as f:\n",
    "        meaningless_word = f.read().splitlines()\n",
    "        # print(meaningless_word)\n",
    "    result = []\n",
    "    # 筛选词语\n",
    "    for i in word_list:\n",
    "        if i not in meaningless_word:\n",
    "            result.append(i.replace(' ', ''))\n",
    "    return result\n",
    "\n",
    "# 词频统计\n",
    "def word_counter(words):\n",
    "    # 使用Count计数方法\n",
    "    words_counter = Counter(words)\n",
    "    # 将Counter类型转换为列表\n",
    "    words_list = words_counter.most_common(100)\n",
    "    return words_list\n",
    "\n",
    "# 生成词云\n",
    "def create_wordcloud(json_file, title='词云', column='title'):\n",
    "    text = get_text(json_file, column=column)\n",
    "    clear_word = split_word(text)\n",
    "    data = word_counter(clear_word)\n",
    "    wd = WordCloud()\n",
    "    wd.add(series_name=title, data_pair=data, word_size_range=[40, 150])\n",
    "    wd.set_global_opts(title_opts=opts.TitleOpts(title=\"你的文章词云\", subtitle=\"基于你的博客数据生成\", title_textstyle_opts=opts.TextStyleOpts(font_size=23)), tooltip_opts=opts.TooltipOpts(is_show=True))\n",
    "    # wd.render_notebook()\n",
    "    wd.render('topic_wordcloud.html')\n",
    "\n",
    "# 生成折线图\n",
    "def create_postdate_line(json_file, title='折线图', column='postdate'):\n",
    "    df_json = pd.read_json('cnblog_article.json', encoding='utf-8')\n",
    "    postdate_month_list = []\n",
    "    for i in df_json[column]:\n",
    "        postdate_month_list.append('-'.join(i.split('-')[:-1]))\n",
    "    date_counter = Counter(postdate_month_list)\n",
    "    line = Line()\n",
    "    x_data = [i for i in date_counter]\n",
    "    y_data = [date_counter[i] for i in date_counter]\n",
    "    line.add_xaxis(x_data)\n",
    "    line.add_yaxis(series_name=\"发文数量\", y_axis=y_data)\n",
    "    line.set_global_opts(title_opts=opts.TitleOpts(title=\"你的发文数量\", subtitle=\"基于你的博客数据生成\"))\n",
    "    line.render('postdate_line.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "create_wordcloud('cnblog_article.json', title='你的创作领域词云', column='title')\n",
    "create_postdate_line('cnblog_article.json', title='发文时间线', column='postdate')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}